package com.design.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.reggie.dto.SetmealDto;
import com.design.reggie.entity.Setmeal;
import com.design.reggie.entity.SetmealDish;
import com.design.reggie.service.SetmealDishService;
import com.design.reggie.service.SetmealService;
import com.design.reggie.mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author JPY
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2023-02-02 16:29:16
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时在套餐菜品表插入相关数据
     * @param setmealDto
     */
    @Override
    @Transactional
    public void addWithSetmealDish(SetmealDto setmealDto) {
        // 保存套餐
        this.save(setmealDto);

        // 保存套餐菜品对应表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> list = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(String.valueOf(setmealDto.getId()));
            list.add(setmealDish);
        }
        setmealDishService.saveBatch(list);
    }

    /**
     * 查询套餐和所包含的菜品，用于回显
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(long setmealId) {
        // 查询套餐信息
        Setmeal setmeal = this.getById(setmealId);
        // 拷贝属性
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 修改套餐信息，同时更新setmeal_dish表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        // 更新套餐基础信息
        this.updateById(setmealDto);
        Long setmealId = setmealDto.getId();

        // 删除setmeal_dish的该套餐信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(queryWrapper);

        // 重新添加setmeal_dish的该套餐信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            SetmealDish newDish = new SetmealDish();

            // 先拷贝基础信息，再设置setmealId，否则setmealId会被清空
            BeanUtils.copyProperties(setmealDish, newDish);
            newDish.setSetmealId(String.valueOf(setmealId));

            setmealDishService.save(newDish);
        }
    }

    /**
     * 根据ids删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            // 删除套餐
            this.removeById(id);

            // 删除套餐菜品表中的相关数据
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(queryWrapper);

        }
    }
}
